package com.yuerenjf.yuerenjinfu.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yuerenjf.yuerenjinfu.R;
import com.yuerenjf.yuerenjinfu.activity.MainActivity;
import com.yuerenjf.yuerenjinfu.http.Constants;
import com.yuerenjf.yuerenjinfu.http.Xutils;
import com.yuerenjf.yuerenjinfu.utils.CommonUtils;
import com.yuerenjf.yuerenjinfu.utils.MessageTimer;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/21 0021.
 */

public class RetrieveFragment extends BaseFragment implements View.OnClickListener {

    private static final int SEND_CODE_AGAIN = 1;

    private EditText et_retrieve_username;
    private EditText et_retrieve_code;
    private TextView tv_retrieve_code;
    private TextView tv_retrieve_complete;
    private EditText et_retrieve_pwd;
    private EditText et_retrieve_again;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MainActivity mainActivity = (MainActivity) getActivity();
        mainActivity.setmTitleTextView("找回密码");
        mainActivity.setLogoImageShow(false);

    }

    @Override
    public void prepareData() {

    }

    @Override
    public View prepareView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.activity_retrieve, null);
        et_retrieve_username = (EditText) view.findViewById(R.id.et_retrieve_username);
        et_retrieve_code = (EditText) view.findViewById(R.id.et_retrieve_code);
        tv_retrieve_code = (TextView) view.findViewById(R.id.tv_retrieve_code);
        tv_retrieve_complete = (TextView) view.findViewById(R.id.tv_retrieve_complete);
        et_retrieve_pwd = (EditText) view.findViewById(R.id.et_retrieve_pwd);
        et_retrieve_again = (EditText) view.findViewById(R.id.et_retrieve_again);

        tv_retrieve_code.setOnClickListener(this);
        tv_retrieve_complete.setOnClickListener(this);


        return view;
    }

    @Override
    public void onloadData(View view) {

    }

    @Override
    public void adapterScreen() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_retrieve_code:
                if (!CommonUtils.isMobileNO(et_retrieve_username.getText().toString())) {
                    Toast.makeText(getActivity(), "手机号码格式不正确---", Toast.LENGTH_SHORT).show();
                    return;
                }
                sendCode(et_retrieve_username.getText().toString());
                break;
            case R.id.tv_retrieve_complete:
                if (!CommonUtils.isMobileNO(et_retrieve_username.getText().toString())) {
                    Toast.makeText(getActivity(), "手机号码格式不正确", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(et_retrieve_code.getText().toString())) {
                    Toast.makeText(getActivity(), "验证码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(et_retrieve_pwd.getText().toString()) || TextUtils.isEmpty(et_retrieve_again.getText().toString())) {
                    Toast.makeText(getActivity(), "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!et_retrieve_pwd.getText().toString().equals(et_retrieve_again.getText().toString())) {
                    Toast.makeText(getActivity(), "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }


                retrieve();
                break;
            case R.id.ll_back:
                break;
        }
    }

    private void retrieve() {
        Map<String, String> map = new HashMap<>();
        map.put("phone", et_retrieve_username.getText().toString());
        map.put("password", et_retrieve_pwd.getText().toString());
        map.put("captcha", et_retrieve_code.getText().toString());
        map.put("pwd", et_retrieve_again.getText().toString());
        Xutils.getInstance().get(getActivity(), Constants.URL_RETRIEVE, map, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                Log.e("sendCode", result + "------");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    int status = jsonObject.getInt("status");
                    String message = jsonObject.getString("data");
                    if (status == 200) {
                        Toast.makeText(getActivity(), "密码修改成功", Toast.LENGTH_SHORT).show();
                    } else if (status == 300 || status == 400) {
                        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "修改失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailer(int status, String result) {

            }

            @Override
            public void onFinish() {

            }
        });

    }

    private void sendCode(String phone) {
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        Xutils.getInstance().post(getActivity(), Constants.URL_RETRIEVE_GET_CODE, map, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                Log.e("sendCode", result + "------");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    int status = jsonObject.getInt("status");
                    if (status == 200) {
                        Toast.makeText(getActivity(), "验证码发送成功", Toast.LENGTH_SHORT).show();
                        new MessageTimer(tv_retrieve_code, 90000, 1000).start();

                    } else if (status == 400) {
                        String message = jsonObject.getString("message");
                        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "发送失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailer(int status, String result) {

            }

            @Override
            public void onFinish() {

            }
        });

    }
}
